package com.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mall.entity.MallCarousel;
import com.mall.entity.PageBean;
import com.mall.mapper.MallCarouselMapper;
import com.mall.service.MallCarouselService;
import com.mall.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MallCarouselServiceImpl implements MallCarouselService {

    private MallCarouselMapper mallCarouselMapper;

    @Autowired
    public MallCarouselServiceImpl( MallCarouselMapper mallCarouselMapper ) {
        this.mallCarouselMapper = mallCarouselMapper;
    }

    @Override
    public MallCarousel getMallCarouselById(String carouselId) {
        return mallCarouselMapper.getMallCarouselById(carouselId);
    }

    @Override
    public PageBean getMallCarouselList(Integer currentPage, Integer pageSize) {
        // 1.设置分页
        PageHelper.startPage(currentPage,pageSize);
        // 2. 执行查询
        List<MallCarousel> mallCarouselList = mallCarouselMapper.getList();
        Page<MallCarousel> p = (Page<MallCarousel>) mallCarouselList;
        // 3.封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());

        return pageBean;
    }

    @Override
    public int deleteMallCarouselById(String carouselId) {
        return mallCarouselMapper.deleteMallCarouselById(carouselId);
    }

    @Override
    public int addMallCarousel(MallCarousel mallCarousel) {
        String id;
        while (true) {
            // 随机生成8位的字符串
            id = StringUtil.getUUIDRandomString(8);
            MallCarousel temp = mallCarouselMapper.getMallCarouselById(id);
            if(temp == null) break;
        }
        mallCarousel.setCarouselId(id);
        mallCarousel.setIsDelete(0);
        return mallCarouselMapper.addMallCarousel(mallCarousel);
    }

    @Override
    public int updateMallCarousel(MallCarousel mallCarousel) {
        return mallCarouselMapper.updateMallCarousel(mallCarousel);
    }

    /**
     *根据轮播图的ArrayList<String>类型的listCarouselId来进行逻辑删除（更新轮播图is_delete）
     * @param listCarouselId
     * @return
     */
    @Override
    public int updateListMallCarouselById(ArrayList<String> listCarouselId) {
        return mallCarouselMapper.updateListMallCarouselById(listCarouselId);
    }
}
